package com.kelin.calendarlistview.library;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.utils.Color;

import ohos.app.Context;

import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * LoadingFooter
 */
public class LoadingFooter {
    protected Component mLoadingFooter;

    protected RoundProgressBar mLoadingProgressBar;

    protected Text mLoadingText;

    protected State mState = State.Idle;

    private static AnimatorProperty rotateAnimator;

    /**
     * State
     *
     */
    public enum State {
        Idle, TheEnd, Loading
    }

    /**
     * LoadingFooter instance
     *
     * @param context 上下文
     */
    public LoadingFooter(Context context) {
        mLoadingFooter = LayoutScatter.getInstance(context).parse(
                ResourceTable.Layout_listfooter_loading, null, false);
        mLoadingFooter.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

            }
        });
        mLoadingProgressBar = (RoundProgressBar) mLoadingFooter
                .findComponentById(ResourceTable.Id_progress_loading);
        mLoadingText = (Text) mLoadingFooter
                .findComponentById(ResourceTable.Id_text_loading);
        mLoadingFooter.setVisibility(Component.HIDE);

    }

    protected void init(String noMoreDataMessage, int loadingTextColor) {
        mLoadingText.setText(noMoreDataMessage);

        mLoadingText.setTextColor(new Color(loadingTextColor));
        setState(State.Idle);
    }

    /**
     * getView
     * @return Component
     */
    public Component getView() {
        return mLoadingFooter;
    }

    /**
     * getState
     * @return State
     */
    public State getState() {
        return mState;
    }

    /**
     * setState
     * @param state State
     * @param delay delay
     */
    public void setState(final State state, long delay) {
        new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
            @Override
            public void run() {
                setState(state);
            }
        }, delay);
    }

    /**
     * setState
     * @param status status
     */
    public void setState(State status) {
        if (mState == status) {
            return;
        }
        mState = status;

        mLoadingFooter.setVisibility(Component.VISIBLE);

        switch (status) {
            case Loading:
                mLoadingProgressBar.setVisibility(Component.VISIBLE);
                startRotateAnimation(mLoadingProgressBar);
                mLoadingText.setVisibility(Component.HIDE);
                break;
            case TheEnd:
                mLoadingText.setVisibility(Component.VISIBLE);
                mLoadingProgressBar.setVisibility(Component.HIDE);
                stopRotateAnimation();
                startAlphaAnimation(mLoadingText, 100);
                break;
            default:
                mLoadingFooter.setVisibility(Component.HIDE);
                break;
        }
    }

    /**
     * startAlphaAnimation
     * @param component Component
     * @param duration duration
     */
    public static void startAlphaAnimation(Component component, long duration) {
        component.setAlpha(1);
        AnimatorProperty animator = component.createAnimatorProperty();
        animator.alpha(0.2f);
        animator.setDuration(duration);
        animator.start();
    }

    /**
     * startRotateAnimation
     * @param component Component
     */
    public static void startRotateAnimation(Component component) {
//        component.setRotation(0);
        rotateAnimator = component.createAnimatorProperty();
        rotateAnimator.setCurveType(Animator.CurveType.ANTICIPATE_OVERSHOOT);
        rotateAnimator.rotate(360);
        rotateAnimator.setDuration(2000);
        rotateAnimator.setLoopedCount(2000);
        rotateAnimator.start();
    }

    /**
     * stopRotateAnimation
     */
    public static void stopRotateAnimation() {
        if (rotateAnimator == null) {
            return;
        }
        rotateAnimator.stop();
        rotateAnimator.release();
        rotateAnimator = null;
    }
}
